package MonitorUI;

import Communication.*;
import Database.*;
import Enclosure.*;
import SetupUI.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class ZooMonitorPanel extends JFrame implements Runnable {
    public JCheckBoxMenuItem setupMenuItem;
    ZooDataHandler zooDataHandler = new ZooDataHandler();
    ZooServer zooServer = new ZooServer(5555);
    ZooSetupPanel zooSetupPanel = new ZooSetupPanel();
    JPanel mainPanel;
    JScrollPane scrollPane;
    int position = 0;

    public ZooMonitorPanel() throws Exception {
        this.setTitle("Zoo Monitor");
        this.setSize(650, 460);
        this.setLocation(100, 100);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setJMenuBar(createMenu());

        packPanels();

        this.setVisible(true);
        this.run();
        zooSetupPanel.addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {
                System.out.println("CLOSING");}
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("CLOSING");
            }
            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("CLOSING");
                setupMenuItem.setState(false);}
            @Override
            public void windowIconified(WindowEvent e) {
                System.out.println("CLOSING");}
            @Override
            public void windowDeiconified(WindowEvent e) {
                System.out.println("CLOSING");}
            @Override
            public void windowActivated(WindowEvent e) {
                System.out.println("CLOSING");}
            @Override
            public void windowDeactivated(WindowEvent e) {
                System.out.println("CLOSING");
                setupMenuItem.setState(false);
            }
        });
    }


    public JMenuBar createMenu() {
        JMenuBar menuBar = new JMenuBar();

        JMenu fileMenu  = new JMenu("File");
        menuBar.add(fileMenu);
        JMenuItem exitMenuItem = new JMenuItem("Exit");
        exitMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        fileMenu.add(exitMenuItem);

        JMenu setupMenu  = new JMenu("Setup");
        menuBar.add(setupMenu);
        setupMenuItem = new JCheckBoxMenuItem("Show Setup Interface");
        setupMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                zooSetupPanel.setVisible(true);
            }
        });
        setupMenu.add(setupMenuItem);

        return menuBar;
    }


    public void packPanels() throws Exception {
        mainPanel = null;
        scrollPane = null;
        mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.ipadx = 195;
        constraints.ipady = 195;

        for (Enclosure enclosure : zooDataHandler.getAllEnclosures()) {
            if (constraints.gridx == 3) {
                constraints.gridx = 0;
                constraints.gridy++;
            }
            EnclosureView ev = new EnclosureView(enclosure);
            ev.addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(MouseEvent e) {}
                @Override
                public void mousePressed(MouseEvent e) {}
                @Override
                public void mouseReleased(MouseEvent e) {
                    try {
                        zooServer.sendUpdate();
                    } catch (Exception err) {
                        System.out.println(err);
                    }
                }
                @Override
                public void mouseEntered(MouseEvent e) {}
                @Override
                public void mouseExited(MouseEvent e) {}
            });
            mainPanel.add(ev, constraints);
            constraints.gridx++;
        }
        //System.out.println(position);
        scrollPane = new JScrollPane(mainPanel);
        this.add(scrollPane);
        //System.out.println(scrollPane.getVerticalScrollBar().getModel().getValue());
        //scrollPane.getVerticalScrollBar().getModel().setValue(302);
        //System.out.println(scrollPane.getVerticalScrollBar().getModel().getValue());
        this.setVisible(true);
    }


    public void reDrawUI() throws Exception{
        //this.position = scrollPane.getVerticalScrollBar().getModel().getValue();
        this.getContentPane().removeAll();
        mainPanel = null;
        packPanels();
        this.repaint();
    }



    @Override
    public void run() {
        try {
            System.out.println("START LISTENING");
            zooServer.listenForClient();
            while (true) {
                System.out.println("Server LISTENING");
                zooServer.receiveUpdate();
                System.out.println("SHOULD UPDATE HERE");
                reDrawUI();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }


    public static void main(String[] args) throws Exception {
        new ZooMonitorPanel();
    }
}
